package com.kate.mall.promotion.service.impl;

import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.Optional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kate.mall.common.utils.PageUtils;
import com.kate.mall.common.utils.Query;

import com.kate.mall.promotion.dao.PromotionActivityGoodsRelationDao;
import com.kate.mall.promotion.entity.PromotionActivityGoodsRelationEntity;
import com.kate.mall.promotion.service.PromotionActivityGoodsRelationService;


@Service("promotionActivityGoodsRelationService")
public class PromotionActivityGoodsRelationServiceImpl extends ServiceImpl<PromotionActivityGoodsRelationDao, PromotionActivityGoodsRelationEntity> implements PromotionActivityGoodsRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PromotionActivityGoodsRelationEntity> page = this.page(
                new Query<PromotionActivityGoodsRelationEntity>().getPage(params),
                generatorQueryWrapper(params)
        );

        return new PageUtils(page);
    }
    @Override
    public Optional<Integer> findActivityIdByGoodsId(Integer goodsId) {
        PromotionActivityGoodsRelationEntity promotionActivityGoodsRelationEntity = this.baseMapper.selectOne(new QueryWrapper<PromotionActivityGoodsRelationEntity>().lambda().eq(PromotionActivityGoodsRelationEntity::getGoodsId, goodsId));
        return Optional.ofNullable(promotionActivityGoodsRelationEntity==null?-1:promotionActivityGoodsRelationEntity.getPromotionActivityId());
    }

    public QueryWrapper generatorQueryWrapper(Map<String, Object> params) {//is_hot=1
        QueryWrapper<PromotionActivityGoodsRelationEntity> queryWrapper = new QueryWrapper<>();
        if (params.containsKey("goods_id")) {
            //where is_hot=1
            queryWrapper.eq("goods_id", Integer.parseInt((String) params.get("goods_id")));
        }
        return queryWrapper;
    }
}